package Hxw3DEngine.core.materials.shader.filters {

	import Hxw3DEngine.core.Resource.texture.ResourceTexture;
	
	import Hxw3DEngine.core.materials.shader.utils.FcRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.FsRegisterLabel;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterCache;
	import Hxw3DEngine.core.materials.shader.utils.ShaderRegisterElement;
	import Hxw3DEngine.core.Resource.geometry.ResourceGeometry;
	
	/**
	 * 普通贴图 
	 * @author Neil
	 */	
	public class TextureMapFilter extends Filter3D {

		private var label : FsRegisterLabel;
		private var data  : Vector.<Number>;
		
		/**
		 * 贴图 
		 * @param texture
		 */		
		public function TextureMapFilter(texture:ResourceTexture) {
			super("TextureMapFilter");
			this.priority = 15;
			this.data  = Vector.<Number>([1, 1, 0, 0]);
			this.label = new FsRegisterLabel(texture);
		}
		
		public function get repeatX() : Number {
			return data[0];
		}
		
		public function set repeatX(value : Number) : void {
			data[0] = value;
		}
		
		public function get repeatY() : Number {
			return data[1];
		}
		
		public function set repeatY(value : Number) : void {
			data[1] = value;
		}
		
		public function get offsetX() : Number {
			return data[2];
		}
		
		public function set offsetX(value : Number) : void {
			data[2] = value;
		}
		
		public function get offsetY() : Number {
			return data[3];
		}
		
		public function set offsetY(value : Number) : void {
			data[3] = value;
		}
		
		public function get texture() :ResourceTexture {
			return this.label.texture;
		}
		
		public function set texture(value : ResourceTexture) : void {
			this.label.texture = value;
		}
		
		override public function getFragmentCode(regCache : ShaderRegisterCache) : String {
			super.getFragmentCode(regCache);
			var fs0 : ShaderRegisterElement = regCache.getFs(label,"diffuse纹理");
			var fc0 : ShaderRegisterElement = regCache.getFc(1, new FcRegisterLabel(data),"UV偏移");
			var ft0 : ShaderRegisterElement = regCache.getFt("空闲的");
			// 复制uv数据到ft0
			mov(ft0,regCache.getV(ResourceGeometry.UV0));
			// uv * repeat
			mul(ft0.xy,ft0.xyxy,fc0.xyxy);
			// uv * repeat + offset
			add(ft0.xy,ft0.xyxy,fc0.zwzw);
			// sample
			tex(regCache.oc,ft0.xy,fs0,description(label.texture));
			regCache.removeFt(ft0);
			return code;
		}
		
		
	}
}
